package com.vectorimage.upload;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

/**
 * 上传图片
 */
public class Upload extends HttpServlet{
	private static final long	serialVersionUID	= 1L;

	public Upload(){
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doSaveAttach(request, response);
	}

	// 保存uploadify上传附件
	public void doSaveAttach(HttpServletRequest request, HttpServletResponse response){
		try{
			String savePath = getFileUrl(request);
			DiskFileItemFactory fac = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(fac);
			upload.setHeaderEncoding("utf-8");
			List fileList = upload.parseRequest(request);
			Iterator it = fileList.iterator();
			String name = "";
			String extName = "";
			String fullFileName = "";
			String fileSize = "";
			while(it.hasNext()){
				FileItem item = (FileItem)it.next();
				if( !item.isFormField()){
					name = item.getName();
					if(name == null || name.trim().equals("")){
						continue;
					}
					if(name.lastIndexOf(".") >= 0){
						extName = name.substring(name.lastIndexOf("."));
					}
					File file = null;
					do{
						name = String.valueOf(System.currentTimeMillis());
						fileSize = String.valueOf(item.getSize());
						file = new File(savePath + "/" + name + extName);
					}while(file.exists());
					// 文件名赋值给fullFileName，回传前台
					fullFileName = name + extName;
					File saveFile = new File(savePath + "/" + name + extName);
					item.write(saveFile);
					// 访问日志
					//SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					//String time = formater.format(new Date());
					// Logger log = Logger.getLogger(Upload.class);
					// log.info("Uploadify上传附件：【" + fullFileName + "】\t" + time + "\t" + Tool.getIP(request) + "\t" + Tool.getAddressByIP(Tool.getIP(request)));
				}
			}
			// 回传参数到前台
			response.setCharacterEncoding("utf-8");
			response.getWriter().write("../FileData/imgs/" + fullFileName);
			System.out.println("../FileData/imgs/" + fullFileName);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	// 文件存储路径
	public String getFileUrl(HttpServletRequest request){
		String savePath = request.getSession().getServletContext().getRealPath("../FileData/imgs");
		File f1 = new File(savePath);
		if(!f1.exists()){
			f1.mkdirs();
		}
		return savePath;
	}

}